package com.ling.tools.login.utils;

import com.ling.tools.login.entity.User;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

/**
 * File Name: PwdUtils
 * Date: 2020/2/28 15:09
 * Author: liangling
 * Description 用户注册密码加密加盐
 */
public class PwdUtils {

    private static final String algorithmName = "md5"; 
    private static final int hashIterations = 2;


    
 /**
  *  使用SimpleHash的构造器。四个参数
  *  algorithmName：要加密的算法名称
  *  source：要加密的元数据，比如密码
  *  salt：要一起加密的数据
  *  hashIterations： 代表hash迭代的次数，我设置的2，相当于md5(md5(""))
  */
    public static void pwdEncryption(User user){
        String salt = new SecureRandomNumberGenerator().nextBytes().toHex();//生成盐
        user.setSalt(salt);
        String pwd = new SimpleHash(algorithmName,user.getPassword(), ByteSource.Util.bytes(user.getSalt()),hashIterations).toString();//生成密文
        user.setPassword(pwd);

    }
}
